<template>
  <div>
    <div class="listView" v-if="showType==0">
      <div class="talkList">
        <div
          class="talkItem"
          v-for="(item, index) in data"
          :key="index"
          @click.stop="jump(item)"
        >
          <img class="headIcon" :src="item.img" alt="" />
          <div class="content">
            <div class="name">{{ item.toName }}</div>
            <div class="text">{{ item.content }}</div>
          </div>
          <div class="time">{{ item.time }}</div>
        </div>
      </div>
      <Tabbar :indexs="2"></Tabbar>
    </div>

    <div class="detailView" v-if="showType==1">
      <div class="navihead">
        <img class="back" src="@/assets/back.png" @click="back()" alt="" />
        <span>{{ activeData.data.toName }}</span>
        <div class="back" @click.stop="gotoUserList()">
          <img src="../../assets/img/userList.png" alt="" srcset="">
        </div>
      </div>
      <div class="msgView">
        <div
          :class="item.fromId != userData.user_id ? 'msgItem' : 'msgItem myMsgItem'"
          v-for="(item, index) in activeData.data.msgList"
          :key="index"
        >
          <img class="headICon" :src="item.headIcon" alt="" />
          <div class="message">{{ item.content }}</div>
        </div>
      </div>
      <wjhInput @submit="submitMsg"></wjhInput>
    </div>

    <div class="userListView" v-if="showType==2">
      <div class="navihead">
        <img class="back" src="@/assets/back.png" @click="back()" alt="" />
        <span>在线列表</span>
        <div class="back">
          
        </div>
      </div>
      <div class="userList" v-for="(item,index) in userList.data" :key="index" @click.stop="gotoAlong(item)">
        <div class="userItem">
          <img class="headIcon" :src="item.headIcon" alt="" srcset=""  >
          <span class="userName">{{item.fromName}}</span>
        </div>
        
      </div>
      
    </div>

  </div>
</template>
<script>
import { reactive, ref,getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import wjhInput from "../../components/input.vue";
export default {
  components: {
    wjhInput,
  },
  setup() {
    const {proxy}=getCurrentInstance()
    const ws = new WebSocket("ws://127.0.0.1:8540/test");
    const router = useRouter();
    const data = reactive([
      {
        img: require("../../assets/game.png"),
        toId: "all",
        toName: "公共聊天室",
        content: "hahhaa",
        time: "11:30",
        msgList: [],
      },
    ]);
    let userList=reactive({
      data:{
        id:'',
        headIcon:'',
        userName:''
      }
    })
    let activeData = reactive({
      data:{}
    });
    let msg = ref("");
    let showType = ref(0);
    let userData = JSON.parse(sessionStorage.getItem("user_data"));
    ws.onopen = () => {
      // 打开WebSocket连接后立刻发送一条消息:
      ws.send(
        JSON.stringify({
          fromId: userData.user_id,
          fromName: userData.name,
          content: userData.user_id + "我登陆了",
          type: "login",
          toId: "all",
          toName: "公共聊天室",
          headIcon: userData.head_icon,
        })
      );
    };
    ws.onmessage = (msg) => {
      //获取所有信息展示在客户端
      let getmsg = JSON.parse(msg.data);
      console.log(getmsg)
      let hasRoom= data.some((item) => {
        if (item.toId === getmsg.toId||item.toId === getmsg.fromId) {
          item.content = getmsg.content;
          item.msgList.push(getmsg);
          return true;
        }
      });
      if(!hasRoom){
         data.push({
        img: getmsg.headIcon,
        toId: getmsg.toId,
        toName: getmsg.toName,
        content: getmsg.content,
        time: proxy.$time(Math.round(new Date() / 1000)),
        msgList: [getmsg],
      },) 
      }
    };
    //客户端监控异常信息
    ws.onerror = (error) => {
      if (error) {
        console.log(error);
      }
    };
    ws.onclose = () => {
      console.log(`下线了`);
    };
    const send = () => {
      ws.send(
        JSON.stringify({
          fromId: userData.user_id,
          fromName: userData.name,
          content: userData.user_id + "我登陆了",
          type: "msg",
          toId: "all",
          toName: "公共聊天室",
          headIcon: userData.head_icon,
        })
      );
    };
    const jump = (item) => {
      showType.value++
      activeData.data = item;
    };
    const back = () => {
      showType.value--
    };
    const submitMsg = (text) => {
      ws.send(
        JSON.stringify({
          fromId: userData.user_id,
          fromName: userData.name,
          content: text,
          type: "msg",
          toId: activeData.data.toId,
          toName: activeData.data.toName,
          headIcon: userData.head_icon,
        })
      );
    };
    const gotoUserList=()=>{
      showType.value++
      proxy.$axios.get('http://127.0.0.1:8540/getUser').then(res=>{
        userList.data=res.data.data
      })
    }
    const gotoAlong=(item)=>{
      data.push({
        img: item.headIcon,
        toId: item.fromId,
        toName: item.fromName,
        content: "",
        time: proxy.$time(),
        msgList: [],
      })
      showType.value=0
    }
    return { data, activeData, showType,userData,userList, send, jump, back, submitMsg,gotoUserList,gotoAlong };
  },
};
</script>

<style lang="less" scoped>
.detailView {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100vh;
  background: #fff;
}
.talkItem {
  display: flex;
  padding: 30rem;
  border-bottom: 1px solid #f8f8f8;
  position: relative;
  color: #a5a5a5;
  .headIcon {
    width: 100rem;
    height: 100rem;
    margin-right: 20rem;
  }
  .content {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    width: 70%;
    overflow: hidden;
    text-overflow: ellipsis;
    .name {
      font-size: 40rem;
      margin-bottom: 15rem;
      color: #000;
    }
    .text {
      font-size: 30rem;
    }
  }
  .time {
    font-size: 30rem;
    position: absolute;
    right: 30rem;
  }
}
.navihead {
  position: sticky;
  top: 0rem;
  background: #ffffff;
  box-sizing: border-box;
  width: 100%;
  height: 98rem;
  padding: 15rem 30rem 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .back {
    width: 36rem;
    height: 36rem;
  }
  span {
    font-size: 36rem;
    font-family: PingFang SC;
    font-weight: bold;
    color: #303133;
  }
}
.msgView {
  height: calc(100vh - 200rem);
  overflow: auto;

  .msgItem {
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    margin: 30rem 0;
    font-size: 30rem;
    .headICon {
      width: 80rem;
      height: 80rem;
      border-radius: 50%;
      overflow: hidden;
      background: #fff;
      margin: 0rem 40rem;
    }
    .message {
      word-break: break-all;
      max-width: 500rem;
      background: #2f96ff;
      padding: 20rem;
      border-radius: 10rem;
    }
  }
  .myMsgItem {
    flex-direction: row-reverse;
    .message {
      background: #2fff65;
    }
  }
}
.back{
  img{
    width: 100%;
  }
}
.userList{
  margin: 30rem;
  width: 750rem;
  display: flex;
  flex-wrap: wrap;
  .userItem{
    
    margin: 30rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    img{
      
      width: 100rem;
      height: 100rem;
      margin-bottom: 20rem;
      border-radius: 20rem;
      overflow: hidden;

    }
    .userName{
      
      font-size: 30rem;
      font-weight: bold;
      overflow: hidden;
      text-overflow: ellipsis;
      width: 100rem;
      text-align: center;

    }
  }
}
</style>